/**
 * Created by lenovo on 17-6-6.
 */

var express = require('express');
var router = express.Router();
var http = require('http');
var http_post = require('http-post');
var checkNotLogin = require('../middlewares/check').checkNotLogin;

router.get('/',checkNotLogin,function(req,res){
    return res.render('signin', { title: 'myblog' });
});

router.post('/',checkNotLogin,function(req,res,next){
    http_post('http://localhost:3000/signin',req.body,function(response){
        var rawData = '';
        response.setEncoding('utf8');
        response.on('data',function(chunk){
            rawData += chunk;
        }).on("end",function(){
            var signinInfo = JSON.parse(rawData);
            if(signinInfo.status ==  200){
                req.session.user = signinInfo.user_id;
                console.log(req.session)
                return res.json({message: signinInfo.message,status: 200})
            }else{
                return res.json({message: signinInfo.message,status: 400})
            }
        })
    }).on("error",function(e){
        console.error(e)
    });
});

module.exports = router;